﻿using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	//Declaraçao de variaveis
	public float PlayerSpeed;
	public GameObject Fire1Prefab;
	public GameObject ExplosionPrefab;
	public static int scores = 0;
	public static int lives = 3;

	// Use this for initialization
	void Start () {
		transform.position = new Vector3 (0, 0, transform.position.z);
	}
	
	// Update is called once per frame
	void Update () {
		float amtToMove = Input.GetAxisRaw ("Horizontal") * PlayerSpeed * Time.deltaTime;
		float amtToMove2 = Input.GetAxisRaw ("Vertical") * PlayerSpeed * Time.deltaTime;
		transform.Translate (Vector3.right * amtToMove);
		transform.Translate (Vector3.up * amtToMove2);

		if (transform.position.x <= -7.4f)
			transform.position = new Vector3 (7.4f, transform.position.y, transform.position.z);

		else if (transform.position.x >= 7.4f)
			transform.position = new Vector3 (-7.4f, transform.position.y, transform.position.z);


		if (transform.position.y <= -1.6f)
			transform.position = new Vector3 (transform.position.x, 9.6f, transform.position.z);

		else if (transform.position.y >= 9.6f)
			transform.position = new Vector3 (transform.position.x, -1.6f, transform.position.z);
	
		
		if (Input.GetKeyDown ("space")) {
			Vector3 position = new Vector3(transform.position.x, transform.position.y + (transform.localScale.y /2));
			Instantiate (Fire1Prefab, position, Quaternion.identity);
		}	
	}//update

	void OnGUI(){
		GUI.Label (new Rect (10, 10, 105, 20), "Pontuaçao: " + PlayerScript.scores.ToString ());
		GUI.Label (new Rect (10, 30, 60, 30), "Vidas: " + PlayerScript.lives.ToString ());
		}//OnGUI

	void OnTriggerEnter(Collider otherObject)
	{
		if (otherObject.tag == "enemy")
		{
			PlayerScript.lives--;
			EnemyScript enemyscript = (EnemyScript)otherObject.gameObject.GetComponent("EnemyScript");
			enemyscript.SetPositionAndSpeed();
			Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
			StartCoroutine(DestroyShip());
		}
	}//ontrigger}

	IEnumerator DestroyShip()
	{
		Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
		gameObject.renderer.enabled = false;
		transform.position = new Vector3 (0f, transform.position.y, transform.position.z);
		yield return new WaitForSeconds(1.5f);
		gameObject.renderer.enabled = true;
	}
}